<template>
	<view>
		<!-- <z-nav-bar title="实名认证"></z-nav-bar> -->
		<!-- 公共组件-每个页面必须引入 -->
		<!--<public-module></public-module>-->
		<uni-forms :value="eduStudent" ref="form" validate-trigger="bind" err-show-type="undertext">
			<!-- 手机号验证 -->
			<view class="swiper">
				<view class="forms">
					<view class="title-bar">手机号验证</view>
					<uni-forms-item label="真实姓名" name="studentName" required>
						<uni-easyinput type="text" v-model="eduStudent.studentName" placeholder="请输入" />
					</uni-forms-item>
					<uni-forms-item label="手机号码" name="cellNo" required>
						<uni-easyinput type="text" v-model="eduStudent.cellNo" placeholder="请输入" />
					</uni-forms-item>
					<uni-forms-item label="验证码" name="yzm" required>
						<uni-row>
							<uni-col :span="11">
								<input type="text" v-model="eduStudent.yzm" @blur="binddata('yzm', $event.detail.value)"
									placeholder="请输入" />
							</uni-col>
							<uni-col :span="3"><text class="line">|</text></uni-col>
							<uni-col :span="10"> <text :class="['getyzm',(readonly || !checkPhone) &&'gray']"
									@click="getyzm">{{codeText}}</text></uni-col>
						</uni-row>
					</uni-forms-item>
				</view>
			</view>
		</uni-forms>
		<!-- 身份证照片 -->
		<view class="swiper">
			<view class="forms">
				<view class="title-bar">身份证照片</view>
					<view class="camera" @tap="ChooseImage('Up')">
						<image class="up-img" :src="imgObjUp" mode="aspectFill"></image>
						<view v-if="idcard">
						<uni-icons :type="item.icon" size="30" color="#969696"></uni-icons>
						<text>轻点上传身份证正面</text>		
						</view>
						<view v-else>
							
						</view>
					</view>
					<view class="camera" @tap="ChooseImage('Down')">
						<image class="up-img" :src="imgObjDown" mode="aspectFill"></image>
						<view v-if="idcard">
						<uni-icons :type="item.icon" size="30" color="#969696"></uni-icons>
						<text>轻点上传身份证反面</text>		
						</view>
						<view v-else>
						</view>
					</view>
			</view>
		</view>
		<button type="primary" class="btn" @click="submitForm">立即认证</button>
	</view>
</template>

<script>
	import base from '@/config/baseUrl'
	
	export default {
		data() {
			let _this = this
			return {
				modelId: undefined,
				idcard:true,
				readonly: false,
				codeText: '获取验证码',
				checkPhone: false,
				imgObjUp: '',
				imgObjDown: '',
				uuid: '',
				idCard:[
					{text:'轻点上传身份证正面',icon:'camera'},{text:'轻点上传身份证反面'}
				],
				eduStudent: {
					studentName: '',
					cellNo: '',
					yzm: '',
				},
				rules: {
					// 对jobNum字段进行必填验证
					studentName: {
						rules: [{
							required: true,
							errorMessage: '请输入姓名'
						}]
					},
					// 对cell字段进行必填验证
					cellNo: {
						rules: [{
							required: true,
							errorMessage: '请输入手机号码'
						}, {
							validateFunction: async function(rule, value, data,
							callback) { //自定义校验不能配置rules=rules在onReady生命周期调用组件的setRules方法绑定验证规则
								let iphoneReg = /^1[0-9]{10}$/
								if (!iphoneReg.test(value)) {
									callback('手机号码格式不正确，请检查')
									return
								}
								let params = {
									type: '0',
									data: value
								}
								let res = await _this.$api.common.checkStudentRegist(params)
								if(res.code == 200) {
									return true
								} else {
									return new Error('当前手机号已注册，请联系管理员')
								}
							}
						}]
					},
					yzm: {
						rules: [{
							required: true,
							errorMessage: '请输入验证码'
						}]
					}
				}
			};
		},
		watch: {
			'eduStudent.cellNo': {
				handler: function(newVal) {
					let iphoneReg = /^1[0-9]{10}$/
					if (!iphoneReg.test(newVal)) {
						return this.checkPhone = false
					}
					let params = { 
						type: '0',
						data: newVal
					}
					this.$api.common.checkStudentRegist(params).then((res)=>{
						if(res.code != 200) {
							return this.checkPhone = false
						}
					}).catch(err => {
						return this.checkPhone = false
					})
					return this.checkPhone = true
				},
				// immediate: true,
				// deep:true//对象内部的属性监听，也叫深度监听
			}
		},
		computed: {
			disabled() {
				return this.readonly
			},
		},
		onLoad(option) {
			this.modelId = option.modelId
			console.log(this.modelId, 'this.modelId')
		},
		//方法
		onReady() {
			// 需要在onReady中设置规则
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			//选择图片
			ChooseImage(flag) {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'], //从相册选择
					success: (res) => {
						this['imgObj'+ flag] = res.tempFilePaths
						const tempFilePaths = res.tempFilePaths;
							uni.uploadFile({
								url: base.baseUrl + '/common/upload', 
								filePath: tempFilePaths[0],
								name: 'file',
								formData: {
									'user': 'test'
								},
								success: (uploadFileRes) => {
									console.log(uploadFileRes.data);
								}
							});
					}
				});
			},
			onPageJump(url) {
				uni.navigateTo({
					url: url
				});
			},
			submitForm() {
				// 手动提交表单
				console.log(this.eduStudent)
				this.$refs.form.submit().then(async (studenInfo) => {
					console.log('表单返回得值：', studenInfo)
					let params = {
						eduStudent: { ...studenInfo },
						accountId: this.$store.state.userInfo.accountId,
						captchaCode: studenInfo.yzm,
						uuid: this.uuid,
						modelId: this.modelId
					}
					console.log(params, 'params')
					let res = await this.$api.regist.identifyCheck(params)
					console.log(res)
					if(res.code == 200) {
						uni.showModal({
							title: '提示',
							content: '提交成功',
							confirmText: '知道了',
							showCancel: false,
							cancelColor: '#81B435',
							success: (res) => {
								uni.switchTab({
									url: '/pages/home/home'
								});
							}
						});
					}
					// 仅作为示例，非真实参数信息。

				})
			},
			getyzm() {
				if (this.readonly) return;
				this.$api.common.getSmsCode({phoneNo: this.eduStudent.cellNo}).then((res)=>{
					console.log(res)
					this.uuid = res.data.uuid
				})
				const _this = this;
				this.readonly = true;
				this.codeText = '60S后重新获取';
				var s = 60;
				var clear = setInterval(() => {
					s--;
					_this.codeText = s + 'S后重新获取';
					if (s <= 0) {
						clearInterval(clear);
						_this.codeText = '获取验证码';
						_this.readonly = false;
					}
				}, 1000);
			},
		}
	};
</script>
<style lang="scss">
	@import '@/style/mixin.scss';

	page {
		background-color: $uni-bg-color-grey;
	}

	.swiper {
		margin-top: 16px;
	}

	.forms {
		width: 686upx;
		margin: 0 auto;
		padding: 24px 16px;
		background: #FEFFFF;
		border-radius: 16px;

		/deep/ input {
			text-align: right;
			outline: none;
		}

		/deep/ .is-input-border {
			border: 0 !important;
		}

		/deep/ .uni-forms-item__label {
			width: 75px !important;
		}

		// 三级选择器
		/deep/ .vv-input__body {
			text-align: right;
			font-size: 14px;
		}

		/deep/ .vv-input__placeholder {
			color: #777;
			font-size: 14px;
		}

		/deep/.uni-forms-item__content {
			display: flex;
			align-items: center;
			justify-content: flex-end;

			.getyzm {
				color: #81B435;
				text-align: right;
			}

			.gray {
				color: #E5E5E5;
			}

			.uni-col-3 {
				text-align: center;
				color: #E5E5E5;
			}
		}

		.radio-group {
			text-align: right;
		}

		.title-bar {
			font-size: 17px;
			font-weight: bold;
			color: #323232;
			width: 100%;
			padding-bottom: 10px;
			border-bottom: 1px solid #F5F5F5;
		}
		.camera{
			position: relative;
			margin-top:17px;
			height: 168.54px;
			background: #F7F8FA;
			border: 1px dashed #DCDEE0;
			box-sizing: border-box;
			display:flex ;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			overflow: hidden;
			text {
				color:#969696;
				font-size: 13px;
			}
			.up-img {
				width: 100%;
				position: absolute;
				top: 0;
				left: 0;
			}
		}
	}

	.btn {
		background-color: $uni-theme-color;
			width: 686upx;
		border-radius: 30px;
		margin:36px auto;
	}
</style>
